//
//  RegisterViewController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "CaptchaView.h"
#import "ValidateTools.h"
#import "HandleNetLogin.h"
#import "CategoryCell.h"
#import "CKPuslichTool.h"
#import "CheckCodeController.h"
#import "CheckPhoneCodeController.h"
#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *cardType;
@property (weak, nonatomic) IBOutlet UITextField *cardNo;
@property (weak, nonatomic) IBOutlet UITextField *myCode;
@property (weak, nonatomic) IBOutlet CaptchaView *codePic;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel1;
@property (weak, nonatomic) IBOutlet UIButton *codeBg;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel2;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel3;
@property (weak, nonatomic) IBOutlet UIView *ChooseView;
@property (nonatomic, strong)NSArray *typeArray;
@property (weak, nonatomic) IBOutlet UITableView *typeTabel;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel;
@property (weak, nonatomic) IBOutlet UILabel *shuline;
@property (weak, nonatomic) IBOutlet UIButton *chooseBt;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeTop;


@end

@implementation RegisterViewController

-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title=@"新用户注册（1/3）";
    self.typeTabel.hidden=YES;
    self.reminderLabel.text=@"";
    self.reminderLabel1.text=@"";
    self.reminderLabel2.text=@"";
    self.reminderLabel3.text=@"";
    self.userName.layer.cornerRadius=5;
    self.cardType.layer.cornerRadius=5;
    self.cardNo.layer.cornerRadius=5;
    self.codeBg.layer.masksToBounds = NO;
    self.myCode.layer.cornerRadius=5;
    self.nextButton.layer.cornerRadius=5;
    [self setTextfile:self.userName img:[UIImage imageNamed:@"Admin-User"]];
    [self setTextfile:self.cardType img:[UIImage imageNamed:@"zhengjian"]];
    [self setTextfile:self.cardNo img:[UIImage imageNamed:@"zhengjian"]];
    [self.userName  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.cardNo  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.myCode  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self MoveTextfileContent:self.myCode];
    //身份证，护照，港澳回乡证，台胞证，军官证，士兵证，其他
    _typeArray=@[@"身份证",@"护照",@"港澳回乡证",@"台胞证",@"军官证",@"士兵证",@"其他"];
//    self.userName.text=@"韩寿摘";
//    self.cardNo.text = @"330121194302030638";
    // Do any additional setup after loading the view.
//    self.userName.text=@"青青";
//    self.cardType.text=@"其他";
//    self.cardNo.text=@"999999";
    
    
    self.cardType.hidden = YES;
    self.cardNo.hidden = YES;
    self.reminderLabel2.hidden = YES;
    self.shuline.hidden = YES;
    self.chooseBt.hidden = YES;
    _codeTop.constant = 30;
    self.cardType.text=@"其他";
    self.cardNo.text=@"999999";
    
    [HandleNetLogin getCompanyNameSuccess:^(NSDictionary *data) {
       
        if (![data[@"IsApprove"] isEqualToString:@"是"]) {
            self.cardType.hidden = NO;
            self.cardNo.hidden = NO;
            self.reminderLabel2.hidden = NO;
            self.shuline.hidden = NO;
            self.chooseBt.hidden = NO;
            _codeTop.constant = 170;
            self.cardType.text=@"";
            self.cardNo.text=@"";
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];
    
//    if (_isApprove) {
//        self.cardType.hidden = YES;
//        self.cardNo.hidden = YES;
//        self.reminderLabel2.hidden = YES;
//        self.shuline.hidden = YES;
//        self.chooseBt.hidden = YES;
//        _codeTop.constant = 30;
//        self.cardType.text=@"其他";
//        self.cardNo.text=@"999999";
//    }else{
//
//    }
    
    
}
-(void)textFieldDidChange:(UITextField *)textField{
    if (textField==_userName) {
        if (textField.text.length>20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }else if (textField==_myCode) {
        if (textField.text.length>20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }else{
        if (textField.text.length>50) {
            textField.text = [textField.text substringToIndex:50];
        }
    }
    
    
}
- (IBAction)chooseCardType:(UIButton *)sender {
   
    self.typeTabel.hidden=!self.typeTabel.hidden;
    
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)next:(id)sender {
    self.reminderLabel1.text=@"";
    self.reminderLabel2.text=@"";
    self.reminderLabel3.text=@"";
//    CheckCodeController *myVc=(CheckCodeController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"CheckCodeController"];
//    myVc.oldDate=[NSDate date];
//    myVc.myCode=@"";
//    
//    [self.navigationController pushViewController:myVc animated:YES];
//    return;
    //用户姓名
    
    if (self.userName.text.length==0||[self.userName.text rangeOfString:@"不详"].length>0||[self.userName.text rangeOfString:@"不祥"].length>0||[self.userName.text rangeOfString:@"未知"].length>0||[self.userName.text rangeOfString:@"不知道"].length>0||[self.userName.text rangeOfString:@"先生"].length>0||[self.userName.text rangeOfString:@"小姐"].length>0||self.userName.text.length>50) {
        self.reminderLabel1.text=@"*客户姓名不符合要求";
        return;
    }
    
    //证件类型
    if (self.cardType.text.length==0) {
        self.reminderLabel2.text=@"*请选择证件类型";
        return;
    }else if(self.cardNo.text.length==0){
        self.reminderLabel2.text=@"*请输入证件号码";
        return;
    }else {
        if ([self.cardType.text isEqualToString:@"身份证"]&&![ValidateTools validateIDNo:self.cardNo.text]) {
            self.reminderLabel2.text=@"*证件号码错误";
            return;
        }
    }
    
    //验证码
    NSString *text =   [_codePic.changeString lowercaseString];
    if (![text isEqualToString:self.myCode.text]) {
        self.reminderLabel3.text=@"*验证码错误";
        return;
    }else{
//        [_codePic changeString];
//        _myCode.text=@"";
    }
    
    
    //next
    /*
     *点击下一步按钮，调用RegisterCheck，后端通过Json返回信息，如果返回信息为已经注册客户，跳转注册-手机验证页面；如果返回信息为发送初始密码跳转注册-验证码；返回信息为新注册客户，跳转注册-手机验证 拷贝页面
     */
    
    [HandleNetLogin CheckRegisterWithName:self.userName.text IDType:self.cardType.text IDNo:self.cardNo.text Success:^(NSDictionary *data) {
        /***返回消息，三种情况：1.已经注册客户，2.发送初始密码，3.新注册客户
         0代表通过，1代表失败
         **/
        if ([data[@"msg"] isEqualToString:@"已经注册客户"]) {
            self.reminderLabel.text=[NSString stringWithFormat:@"*您已经注册，请通过忘记密码重置您的登录密码"];
        }else if ([data[@"msg"] isEqualToString:@"发送初始密码"]) {
            //已经注册的用户，后台直接发送验证码。去验证页面
            /**
             AccountName
             AccountPhone
             VerificationCode
             **/
            NSDictionary *dictt=data[@"data"][0];
            CheckCodeController *myVc=(CheckCodeController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"CheckCodeController"];
            myVc.oldDate=[NSDate date];
            NSDictionary *infoDic=@{@"name":dictt[@"AccountName"],@"cardType":self.cardType.text,@"cardNo":self.cardNo.text,@"phone":dictt[@"AccountPhone"],@"code":dictt[@"VerificationCode"]};
            myVc.userInfo=infoDic;
            
            [self.navigationController pushViewController:myVc animated:YES];
        }else{
            //去验证手机号码
            CheckPhoneCodeController *vc=(CheckPhoneCodeController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"CheckPhoneCodeController"];
            NSDictionary *infoDic=@{@"name":self.userName.text,@"cardType":self.cardType.text,@"cardNo":self.cardNo.text};
            vc.userinfo=infoDic;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
        
        
        
    } Failure:^(NSString *errorInfo) {
        self.reminderLabel.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }];
    
    
}

-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    //    textfile.layer.borderColor = [UIColor whiteColor].CGColor;
    textfile.layer.cornerRadius = 5;
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _typeArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CategoryCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.NameLabel.text=_typeArray[indexPath.row];

    cell.mImage.hidden=YES;
   
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cardType.text=_typeArray[indexPath.row];
    self.typeTabel.hidden=YES;
}

@end
